const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const { resolve } = path;

const PACK_PATH = path.join(__dirname, './dist');


module.exports = () => {
  
  const isProd = process.env.NODE_ENV === 'production';

  const definePlugin = new webpack.DefinePlugin({
    __DEV__: !isProd
  });

  const buildConfig = {
    mode: process.env.NODE_ENV || 'development',
    entry: {
      app: path.resolve(__dirname, 'src/main.js')
    },
    devtool: !isProd ? 'source-map' : false,
    output: {
      pathinfo: false,
      path: PACK_PATH,
      filename: !isProd ? '[name].js?' : '[name].[chunkhash:6].js'
    },
    watch: !isProd,
    plugins: [
      definePlugin,
      new CleanWebpackPlugin(PACK_PATH, {
        root: __dirname,
        verbose: true,
        dry: false
      }),
      new MiniCssExtractPlugin({
        filename: !isProd ? '[name].css' : '[name].[contenthash:6].css',
        chunkFilename: !isProd ? '[id].css' : '[id].[contenthash:6].css',
      }),
      new HtmlWebpackPlugin({
        template: './index.ejs',
        filename: 'index.html',
        inject: true
      }),
      new HtmlWebpackInlineSourcePlugin()
    ],
    optimization: {
      splitChunks: {
        chunks: 'initial',
        cacheGroups: {
          vendors: {
            test: /[\\/](node_modules|mocha)[\\/]/,
            priority: -10
          },
          plugins: {
            test: /[\\/]equipme-plugins[\\/]/,
            priority: 10
          },
          default: {
            minChunks: 2,
            priority: 20,
            reuseExistingChunk: false
          }
        }
      }
    },
    module: {
      rules: [{
        test: /\.(jsx?|tsx?)$/,
        loader: 'babel-loader',
        options: {
          configFile: resolve('babel.config.js')
        },
        // exclude: /node_modules/,
        include: [
          resolve(__dirname, 'src'),
          resolve(__dirname, 'equipme-plugins'),
          resolve(__dirname, "node_modules/equipme-plugins")
        ]
      },
      {
        test: /\.css$/,
        use: [{
          loader: MiniCssExtractPlugin.loader,
          options: {
            hmr: !isProd,
          },
        },{
          loader: 'css-loader'
        }
        ],
      }, {
        test: /\.(png|jpg|jpeg|gif)$/,
        exclude: /node_modules/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 8192,
            name: !isProd ? './assets/[name].[ext]' : './assets/[name].[hash:6].[ext]'
          }
        }]
      }, {
        test: /\.(woff|woff2|svg|eot|ttf)$/,
        exclude: /node_modules/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 10240,
            name: !isProd ? './assets/fonts/[name].[ext]' : './assets/fonts/[name].[hash:6].[ext]'
          }
        }]
      }
      ]
    },
    devServer: {
      hot: true,
      host: '0.0.0.0',
      publicPath: '/peripheral/',
      contentBase: PACK_PATH,
      proxy: {
        '/m.api': {
          target: 'https://api.dev.pajkdc.com/m.api',
          logLevel: 'debug',
          changeOrigin: true,
          secure: false
        }
      }
    },
    resolve: {
      extensions: ['.js', '.jsx', '.ts', '.tsx'],
      modules: [
        path.resolve('./src'),
        path.resolve('./node_modules')
      ],
      alias: {
        "$":  path.resolve('./equipme-plugins'),
        "@":  path.resolve('./src')
      }
    }
  };

  if (!isProd) {
    buildConfig.plugins.push(
      new webpack.HotModuleReplacementPlugin()
    );
  }
  

  return buildConfig;
};